package com.example.messagemodule.adapter;

import android.text.TextUtils;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.examination.mlib.constants.ProjectConfigure;
import com.example.messagemodule.R;
import com.example.messagemodule.entity.ConversationEntity;
import com.yilijk.base.utils.ALog;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class ConversationAdapter extends BaseQuickAdapter<ConversationEntity.DataBean, BaseViewHolder> {


    public ConversationAdapter(@Nullable List<ConversationEntity.DataBean> data) {
        super(R.layout.convetsation_normal, data);
    }

    //    /**
//     * Same as QuickAdapter#QuickAdapter(Context,int) but with
//     * some initialization data.
//     *
//     * @param data A new list is created out of this one to avoid mutable list
//     */
//    public ConversationAdapter(List<ConversationEntity> data) {
//        super(data);
//        addItemType(0, R.layout.conversationsystemitem);
//        addItemType(1, R.layout.conversationitem);
//    }
    @Override
    protected void convert(BaseViewHolder helper, ConversationEntity.DataBean item) {
        if (!TextUtils.isEmpty(item.getImTeamId())){
            if (!TextUtils.isEmpty(item.getWorkName())){
                helper.setText(R.id.conversationName, item.getWorkName());
            }else{
                helper.setText(R.id.conversationName, "用药咨询");
            }
            Glide.with(mContext).load(item.getDoctorImageUrl())
                    .transform(new CenterCrop(), new CircleCrop())
                    .error(R.drawable.message_doctor_helper_left2)
                    .into((ImageView) helper.getView(R.id.conversationAvatar));
        }else{
            helper.setText(R.id.conversationName, item.getDoctorName());
            Glide.with(mContext).load(item.getDoctorImageUrl())
                    .transform(new CenterCrop(), new CircleCrop())
                    .error(R.drawable.group_head)
                    .into((ImageView) helper.getView(R.id.conversationAvatar));
        }
        helper.setText(R.id.conversationAge, item.getDepartmentName());
        helper.setText(R.id.conversationTime, item.getUpdateTime());
        TextView nameTv = helper.getView(R.id.conversationName);
        nameTv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                nameTv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int maxWidth = helper.getView(R.id.conversationName_LL).getMeasuredWidth() - helper.getView(R.id.conversationAge).getMeasuredWidth() - helper.getView(R.id.conversationTime).getMeasuredWidth();
                nameTv.setMaxWidth(maxWidth);
            }
        });
        helper.setVisible(R.id.conversationUnread, !item.isHaveRead());
        if (item.getImRecordHbaseDTO() != null) {
            ConversationEntity.DataBean.ImRecordHbaseDTOBean imRecordHbaseDTO = item.getImRecordHbaseDTO();
            ALog.e("getDoctorName ===",item.getDoctorName());
            if (imRecordHbaseDTO.getMsgType() == 100) {
                helper.setText(R.id.conversationContent, selectCustomeContent(imRecordHbaseDTO.getCustomEvent(),imRecordHbaseDTO.getCustomExt()));
            } else if (imRecordHbaseDTO.getMsgType() == 0) {
                if (imRecordHbaseDTO.getContent() != null) {
                    helper.setText(R.id.conversationContent, imRecordHbaseDTO.getContent());
                }
            } else if (imRecordHbaseDTO.getMsgType() == 1) {
                helper.setText(R.id.conversationContent, "[图片消息]");
            } else if (imRecordHbaseDTO.getMsgType() == 2) {
                helper.setText(R.id.conversationContent, "[语音消息]");
            } else if (imRecordHbaseDTO.getMsgType() == 3) {
                helper.setText(R.id.conversationContent, "[视频消息]");
            } else if (imRecordHbaseDTO.getMsgType() == 6) {
                helper.setText(R.id.conversationContent, "[文件]");
            }
        } else {
            helper.setText(R.id.conversationContent, "");
        }
    }

    String selectCustomeContent(String customEvent,String customExt) {
        String content = "";
        if (customEvent.equals("chargesList")) {
            content = "[问诊单]";
        } else if (customEvent.equals("delay_charges")) {
                String duration = "";
                try {
                    JSONArray jsonArray = new JSONArray(customExt);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        if (jsonObject.has("duration")) {
                            duration = jsonObject.getString("duration");
                            break;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            content = "医生为您延长" + duration + "分钟问诊时间，你可以继续向医生进行提问";
        } else if (customEvent.equals("patient_update_charges")) {
            content = "您的病情信息已修改成功";
        } else if (customEvent.equals("finish")) {
            content = "[结束问诊]";
        } else if (customEvent.equals("pharmacist_audit")) {
            content = "[系统消息]处方审核通过";
        } else if (customEvent.equals("setting_phone_time")) {
            content = "[系统消息]医生已设置通话时间";
        } else if (customEvent.equals("update_phone_time")){
            content = "[系统消息]医生修改拨打通话时间";
        }else if (customEvent.equals("cancel_appointment_call")){
            content = "[系统消息]取消预约拨打时间";
        }else if (customEvent.equals("miss_phone_call")) {
            content = "[系统消息]电话未接通";
        } else if (customEvent.equals("phone_call_end")) {
            content = "[系统消息]结束通话";
        } else if (customEvent.equals("pharmacist_audit_fail")) {
            content = "[系统消息]处方审核不通过";
        } else if (customEvent.equals("last_charges_template")) {
            content = "[问诊模版结束]";
        } else if (customEvent.equals("stop_send_template")) {
            content = "[问诊模版结束]";
        } else if (customEvent.equals("cancel")) {
            content = "[问诊已取消]";
        } else if (customEvent.equals("refund")) {
            content = "[已退诊]";
        } else if (customEvent.equals("cancel_charges")) {
            content = "[已取消]";
        } else if (customEvent.equals("received")) {
            content = "[已接诊]";
        } else if (customEvent.equals("to_evaluate")) {
            content = "您好，请您对我的问诊服务做出评价";
        } else if (customEvent.equals("patient_evaluate_finish")) {
            content = "医生您好，我已对您的服务作出评价了哦...";
        } else if (customEvent.equals("doctor_evaluate_thanks")) {
            content = "感谢您对我的评价和感谢";
        } else if (customEvent.equals("doctor_prescribing")) {
            content = "[系统消息]医生开方";
        } else if (customEvent.equals("doctor_edit_prescribing")) {
            content = "[系统消息]医生编辑处方";
        } else if (customEvent.equals("finish")) {
            content = "[结束问诊]";
        } else if (customEvent.equals("doctor_prescribing")) {
            content = "[系统消息]处方已开具,药师正在审核中";
        }else if (customEvent.equals("sf_submit")) {
            content = "[随访中]";
        }else if (customEvent.equals("sf_finish")) {
            content = "[随访已结束]";
        }else if (customEvent.equals("charges")){
            content = "请进入聊天界面查看详情";
        }else if (ProjectConfigure.Open.isOpenVideo){
            if (customEvent.equals("start_video")){
                content = "医生发起了视频问诊";
            }else if (customEvent.equals("patient_reject_video")){
                content = "患者拒绝视频通话";
            }else if (customEvent.equals("patient_missed_video")){
                content = "患者未接视频通话";
            }else if (customEvent.equals("video_hang_up")){
                content = "挂断视频问诊";
            }else if (customEvent.equals("patient_missed_video")){
                content = "患者未未接听挂断";
            }else if (customEvent.equals("doctor_reject_video")){
                content = "医生未接通挂断 ";
            }else if (customEvent.equals("patient_reject_video")){
                content = "患者拒绝视频通话";
            }
        }
//        else if (customEvent.equals("")){
//            content = "";
//        }

        ALog.e("getDoctorName ===",customEvent+" === "+content);
        return content;
    }
}
